package cn.ecnu.controller.personal;

import cn.ecnu.common.Relation;
import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.personal.UserMapper;
import cn.ecnu.pojo.personal.Contact;
import cn.ecnu.pojo.personal.Title;
import cn.ecnu.pojo.personal.User;
import cn.ecnu.pojo.resume.ResumeEdu;
import cn.ecnu.pojo.resume.ResumeJob;
import cn.ecnu.service.personal.ContactService;
import cn.ecnu.service.personal.TitleService;
import cn.ecnu.service.resume.ResumeEduService;
import cn.ecnu.service.resume.ResumeJobService;
import cn.ecnu.vo.personal.TitleChange;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@Validated
@RequestMapping("/person")
public class PersonalController {

    @Resource
    ResumeEduService resumeEduService;
    @Resource
    ResumeJobService resumeJobService;
    @Resource
    TitleService titleService;
    @Resource
    ContactService contactService;
    @Resource
    UserMapper userMapper;

    @GetMapping("/card")
    public R getUserInfo(@RequestParam("userId")Integer userId){

        List<ResumeEdu> resumeEdus = resumeEduService.list(Wrappers.<ResumeEdu>lambdaQuery()
                .eq(ResumeEdu::getUserId,userId)
                .orderByDesc(ResumeEdu::getEduStart));
        List<ResumeJob> resumeJobs = resumeJobService.list(Wrappers.<ResumeJob>lambdaQuery()
                .eq(ResumeJob::getUserId,userId)
                .orderByDesc(ResumeJob::getJobStart));
        Title title = titleService.getById(userId);

        Map<String,Object> result = new HashMap<>();
        result.put("edu",resumeEdus);
        result.put("job",resumeJobs);
//        result.put("labels", title.getLabels());
        result.put("title", title);
        return R.data(result);
    }

    @PostMapping("/card")
    public R changeTitle(@RequestBody TitleChange titleChange) {
        Integer eduId, jobId;
        if ((eduId = titleChange.getEduId()) != null) {
            if (titleChange.getJobId()==null){
                ResumeEdu resumeEdu = resumeEduService.getById(eduId);
                Title title = new Title();
                title.setUserId(resumeEdu.getUserId());
                title.setTag1(resumeEdu.getSchool());
                title.setTag2(resumeEdu.getMajor());
                title.setNote("");
                title.setDirection("高校-"+resumeEdu.getQualification()+"生");
                title.setLocation(resumeEdu.getEduLocation());
                titleService.saveOrUpdate(title);
                return R.ok();
            }
        }else if ((jobId = titleChange.getJobId()) != null){
            ResumeJob resumeJob = resumeJobService.getById(jobId);
            Title title = new Title();
            title.setUserId(resumeJob.getUserId());
            title.setTag1(resumeJob.getCompany());
            title.setTag2(resumeJob.getPosition());
            title.setNote(resumeJob.getJobType());
            title.setDirection(resumeJob.getIndustry());
            title.setLocation(resumeJob.getJobLocation());
            titleService.saveOrUpdate(title);
            return R.ok();
        }
        return R.error("更改头衔失败");
    }

}
